import store from '@/store'

// 对于router权限, 只需生成简单的route tree存入redis即可, 无需关心路由对应的组件
export const generateRouterTree = (asyncRoutes) => {
  let routerTree = []
  for (let index in asyncRoutes) {
    let routerItem = generate('', asyncRoutes[index])
    if (routerItem !== null) {
      routerTree.push(routerItem)
    }
  }
  return routerTree
}

// 递归某个router对象
const generate = (parentPermissionCode, routerObj)=> {
  let newRouterObj = {
    permissionCode: '', //由path变化而来
    label: '', //即meta.title
    type: '', // 3个可选值: dir(父级菜单,只作为menu聚合使用,类似于文件夹), menu(具体的侧面菜单,会对应一个vue组件), button(需要权限控制的按钮)
    children: undefined
  }
  if (routerObj.children !== undefined) { //即dir
    if (routerObj.children.length == 1) { //即dir下只包含一个child, 则child自动变为父,该dir路由自动被唯一的child替换
      let parentPath = convertPathToPermissionCode(routerObj.path)
      routerObj = routerObj.children[0] // 子替换父
      return generate(parentPath, routerObj)
    }
    // 否则 继续解析dir@2020-06-09
    let dirPath = routerObj.path
    let dirPermissionCode = convertPathToPermissionCode(dirPath)
    newRouterObj.permissionCode = dirPermissionCode
    if (routerObj.meta == undefined) {// 如果父级路由没有定义meta, 则使用
      console.error("父级路由也必须定义meta信息--->"+ dirPath)
      return null
    }
    newRouterObj.label = routerObj.meta.title
    newRouterObj.type = 'dir'
    newRouterObj.children = []
    for (let index in routerObj.children) {
      if (parentPermissionCode !== '') {
        parentPermissionCode = parentPermissionCode+':'
      }
      newRouterObj.children.push(generate(parentPermissionCode+newRouterObj.permissionCode, routerObj.children[index]))
    }
  } else { //即menu
    let curPath = routerObj.permissionCode || routerObj.path
    newRouterObj.permissionCode = parentPermissionCode+ ':'+convertPathToPermissionCode(curPath)
    if (routerObj.path === '*') {
      return null
    }
    if (routerObj.meta == undefined) {
      console.error("路由对象的meta信息不能为空--->", routerObj.path)
      return null
    }
    newRouterObj.label = routerObj.meta.title
    newRouterObj.type = 'menu'
    // 权限按钮资源
    if (routerObj.authButtons !== undefined) {
      newRouterObj.children = []
      for (let index in routerObj.authButtons) {
        let authBtn = routerObj.authButtons[index]
        let btnRouterObj = {
          permissionCode: authBtn.permissionCode,
          label: authBtn.name,
          type: 'button'
        }
        newRouterObj.children.push(btnRouterObj)
      }
    }
  }
  return newRouterObj
}

const convertPathToPermissionCode = (path)=> {
  let pathStr = String(path)
  if (pathStr.charAt(0) === '/') {
    pathStr = pathStr.slice(1)
  }
  let len = pathStr.length
  if (pathStr.charAt(len-1) === '/') {
    pathStr = pathStr.substring(0, len-1)
  }
  pathStr = pathStr.replace(new RegExp('/', "g"), ':')
  return pathStr
}

const hasPermissionCode = (routerObj, parentPermissionCode)=>{
  const permissionCodes = store.getters.permissionCodes
  if (parentPermissionCode !== '') {
    parentPermissionCode = parentPermissionCode+':'
  }
  const curRouterPermissionCode = parentPermissionCode + convertPathToPermissionCode(routerObj.permissionCode || routerObj.path)
  if (permissionCodes.includes(curRouterPermissionCode)) {
    return true
  } else {
    return false
  }
}

// 根据permissionCodes 去 filter现有路由
export function filterAsyncRoutes2(parentPermissionCode, routes) {
  let parentPermissionCodeOrig = parentPermissionCode
  if (parentPermissionCode !== '') {
    parentPermissionCode = parentPermissionCode + ':'
  }
  const res = []
  routes.forEach(route => {
    const tmp = { ...route }
    const selfPermissionCode = parentPermissionCode + convertPathToPermissionCode(tmp.permissionCode || tmp.path)
    if (!tmp.children) { //路由没有child
      if (tmp.path == '*') { // 任何路由都匹配不上,则自动跳转到404路由
        res.push(tmp)
      } else {
        if (hasPermissionCode(tmp, parentPermissionCodeOrig)){
          res.push(tmp)
        }
      }
    } else if (tmp.children && tmp.children.length==1) { // 路由只有一个child
      if (hasPermissionCode(tmp.children[0], selfPermissionCode)){
        res.push(tmp)
      }
      tmp.children = filterAsyncRoutes2(selfPermissionCode, tmp.children)
    } else { // 路由有多个child
      if (hasPermissionCode(tmp, parentPermissionCodeOrig)){
        res.push(tmp)
      }
      tmp.children = filterAsyncRoutes2(selfPermissionCode, tmp.children)
    }
  })
  return res
}

// let testPath = '/siti/menu-permission/yxy'
// console.log("result-->", convertPathToPermissionCode(testPath))
// console.log("result-->", JSON.stringify(generateRouterTree(asyncRoutes)))
